package com.gmadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmadmin.common.ApiResponse;
import com.gmadmin.entity.SysRole;
import com.gmadmin.entity.dto.SysRoleDto;
import com.gmadmin.payload.RoleQueryCriteria;
import com.gmadmin.service.SysRoleService;
import com.gmadmin.entity.vo.role.SysRoleVo;
import com.gmadmin.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2022/6/13 12:53
 */
@Api(tags = "系统:角色管理")
@RestController
@RequestMapping("api/system/role")
public class SysRoleController {

    private final SysRoleService service;

    public SysRoleController(SysRoleService service) {
        this.service = service;
    }

    /**
     * 查询角色
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询角色")
    public ApiResponse<List<SysRoleVo>> getRoles() {
        return ApiResponse.ofSuccess(service.getRoles());
    }

    @ApiOperation(value = "分页获取角色")
    @GetMapping(value = "page")
    public ApiResponse<PageVo<SysRoleVo>> getListByPage(RoleQueryCriteria criteria, Page<SysRole> page) {
        PageVo<SysRoleVo> roleVos = (PageVo<SysRoleVo>) service.findListByPage(criteria, page);
        return ApiResponse.ofSuccess(roleVos);
    }


    @ApiOperation(value = "更新角色与菜单关系")
    @PutMapping(value = "updateRoleAndMenus")
    public ApiResponse updateRoleAndMenus(@RequestBody SysRoleDto roleDto) {
        service.updateRoleAndMenus(roleDto.getId(), roleDto.getMenuIds());
        return ApiResponse.ofSuccess();
    }

    @ApiOperation(value = "更新角色与api关系")
    @PutMapping(value = "updateRoleAndApis")
    public ApiResponse updateRoleAndApis(@RequestBody SysRoleDto roleDto) {
        service.updateRoleAndApis(roleDto.getId(), roleDto.getApiIds());
        return ApiResponse.ofSuccess();
    }
}
